package com.testdocs.client.error;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.IncompatibleRemoteServiceException;
import com.google.gwt.user.client.rpc.InvocationException;
import com.testdocs.client.common.StatusBar;
import com.testdocs.client.error.i18n.ErrorConstants;

public class RpcErrorHandler {
	private StatusBar statusBar;
	private ErrorConstants errorConstants = (ErrorConstants) GWT.create(ErrorConstants.class);
	
	public RpcErrorHandler(StatusBar statusBar) {
		this.statusBar = statusBar;
	}
	
	public boolean handle(Throwable throwable) {
		boolean result = false;
		if (throwable instanceof InvocationException) {
			statusBar.setError(errorConstants.cannotConnect());
			result = true;
		} else if (throwable instanceof IncompatibleRemoteServiceException) {
			statusBar.setError(errorConstants.outdatedClientVersion());
			result = true;			
		} else if (throwable instanceof ObjectAccessErrorException) {
			statusBar.setError(errorConstants.noPriveleges());
			result = true;						
		}
		return result;
	}
}
